package pierre_feuille_ciseaux;

public class RockPaperScissors {
	
	public Result ResultPlay(RPSEnum p1, RPSEnum p2){
		Result r = Result.LOST;
		
		if(p1.equals(RPSEnum.ROCK) && p2.equals(RPSEnum.SCISSOR)){
			r = Result.WIN;
		}
		
		if(p1.equals(p2)){
			r = Result.TIE;
		}
		
		if(p1.equals(RPSEnum.ROCK) && p2.equals(RPSEnum.PAPER)){
			r = Result.LOST;
		}
		
		if(p1.equals(RPSEnum.SCISSOR) && p2.equals(RPSEnum.PAPER)){
			r = Result.WIN;
		}
		
		if(p1.equals(RPSEnum.PAPER) && p2.equals(RPSEnum.ROCK)){
			r = Result.WIN;
		}
		
		if(p1.equals(RPSEnum.PAPER) && p2.equals(RPSEnum.SCISSOR)){
			r = Result.LOST;
		}
		
		if(p1.equals(RPSEnum.SCISSOR) && p2.equals(RPSEnum.ROCK)){
			r = Result.LOST;
		}
		
		return r;
	}
	
	public Result play(Player p1, Player p2){
		Result r = Result.LOST;
		for(int i=0;i<p1.getNbOfMoves();i++){
			r = ResultPlay(p1.getNextMove(i),p2.getNextMove(i));
			if(r.equals(Result.WIN)){
				p1.setScore();
			}
			if(r.equals(Result.LOST)){
				p2.setScore();
			}
			if(r.equals(Result.TIE)){
				p1.setScore();
				p2.setScore();
			}	
		}
		
		if(p1.getScore()<p2.getScore()){
			r=Result.LOST;
		}
		if(p1.getScore()>p2.getScore()){
			r=Result.WIN;
		}
		if(p1.getScore()==p2.getScore()){
			r=Result.TIE;
		}
		
		return r;
	}
}
